package com.forlove.app.qselect.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.R;
import com.forlove.app.common.base.ui.BaseActivity;
import com.forlove.app.common.utils.CipherUtils;
import com.forlove.app.common.utils.LocationUtils;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.UserManager;
import com.forlove.app.qselect.login.entity.LoginEntity;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.forlove.app.qselect.login.presenter.ResetPwPresenter;
import com.forlove.app.qselect.login.view.ResetPwView;
import com.forlove.app.qselect.login.widget.ErrorMessageView;

import butterknife.BindView;

/**
 * Created by yjl on 2017/10/30.
 */
public class ResetPWActivity extends BaseActivity<ResetPwPresenter> implements ResetPwView, View.OnClickListener {
    @BindView(R.id.view_error_message)
    ErrorMessageView view_error_message;//错误信息提示

    @BindView(R.id.edittext_input_pw)
    AppCompatEditText edittext_input_pw;//输入密码

    @BindView(R.id.edittext_input_pw_again)
    AppCompatEditText edittext_input_pw_again; //再次输入验证码

    @BindView(R.id.bt_register_finish)
    TextView bt_register_finish;

    private String mPW;
    private String mPWAgain;
    private LoginEntity loginEntity;

    @Override
    public int getLayoutId() {
        return R.layout.activity_reset_pw;
    }

    @Override
    public void initView() {
        if (presenter == null) {
            presenter = new ResetPwPresenter(this);
        }
        initEditView();
        bt_register_finish.setOnClickListener(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            loginEntity = (LoginEntity) bundle.getSerializable("loginEntity");
        }
        if (loginEntity == null
                || TextUtils.isEmpty(loginEntity.mobile)
                || TextUtils.isEmpty(loginEntity.valid_token)) {
            this.finish();
        }
    }

    private void initEditView() {
        edittext_input_pw.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mPW = s.toString();
                if (!TextUtils.isEmpty(mPW) && !TextUtils.isEmpty(mPWAgain)) {
                    bt_register_finish.setBackground(getResources().getDrawable(R.drawable.bt_register_nextstep_selector));
                } else {
                    bt_register_finish.setBackground(getResources().getDrawable(R.drawable.bg_register_nextstep_corner_sel));
                }
            }
        });

        edittext_input_pw_again.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mPWAgain = s.toString();
                if (!TextUtils.isEmpty(mPWAgain) && !TextUtils.isEmpty(mPW)) {
                    bt_register_finish.setBackground(getResources().getDrawable(R.drawable.bt_register_nextstep_selector));
                } else {
                    bt_register_finish.setBackground(getResources().getDrawable(R.drawable.bg_register_nextstep_corner_sel));
                }

            }
        });
    }
    

    private void requestSetPW() {
        if (!StringUtils.isNotEmpty(mPW) && !StringUtils.isNotEmpty(mPWAgain)) {
            view_error_message.setVisibility(View.VISIBLE);
            view_error_message.setError("请输入密码！");
            return;
        }
        if (!mPW.equals(mPWAgain)) {
            view_error_message.setVisibility(View.VISIBLE);
            view_error_message.setError("两次密码不一致！");
            return;
        }
        if (!TextUtils.isEmpty(mPW) && (mPW.length() < 6 || mPW.length() > 32)) {
            view_error_message.setVisibility(View.VISIBLE);
            view_error_message.setError("请输入6-32位密码！");
            return;
        }
        loginEntity.password = CipherUtils.md5(mPW);
        String logcation = LocationUtils.getBestLocationString(this);
        loginEntity.location = logcation;
        LogUtil.e("yjl", "经纬度：" + logcation);
        presenter.loadSetPWData(loginEntity);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_register_finish:
                requestSetPW();
                break;
        }
    }

    @Override
    public void showSetPassWordView(UserEntity userEntity) {
        Toast.makeText(this, "设置成功!", Toast.LENGTH_SHORT).show();
        UserManager.Logout();
//        Intent intent = new Intent(this, ChoiceLimitActivity.class);
        Intent intent = new Intent(this, ChoiceNewAgeActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void showErrorData(NetworkManager.ResultCode resultCode, final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                view_error_message.showErrorMsg(msg);
            }
        });
    }
}
